import { Body, Controller, Get, Post, Query, Req } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { AppHistoryService } from './history.service';
import { History } from './history.entity';
import { ApiRes, ApiResType } from 'src/common/decorator/api-res.decorator';
import { AppHistoryListDto } from './dto/list.dto';
import { AddHistoryDto } from './dto/add.dto';
import { RemoveHistoryDto } from './dto/remove.dto';

@ApiTags('app阅读记录模块')
@ApiBearerAuth()
@Controller('app/history')
export class AppHistoryController {
  constructor(private readonly historyService: AppHistoryService) {}

  @ApiOperation({
    summary: '最近阅读-查询阅读记录列表, 默认查询7天',
  })
  @Post('/list')
  @ApiRes(History, ApiResType.PAGE_ARRAY)
  async list(@Body() data: AppHistoryListDto, @Req() req) {
    const { uid } = req.auth;
    return {
      data: await this.historyService.findList(uid, data),
    };
  }

  @ApiOperation({
    summary: '添加阅读记录-阅读文章并关闭时调用',
  })
  @Post('/add')
  async add(@Body() collection: AddHistoryDto, @Req() request) {
    const { uid } = request.auth;
    return {
      data: await this.historyService.create(uid, collection),
    };
  }

  @ApiOperation({
    summary: '删除历史记录',
  })
  @Get('/remove')
  async remove(@Query() data: RemoveHistoryDto) {
    return {
      data: await this.historyService.remove(data.ids),
    };
  }
}
